1 import tkinter as tk
2 import random
3 import time
4
5 class Game(tk.Tk):
6 board = []
7 new_tile_selection = [2,2,2,2,2,2,4]
8 score = 0
9 highscore = 0
10 scorestring = 0
11 highscorestring = 0
12
13 def __init__(self, *args, **kwargs):
14 tk.Tk.__init__(self, *args, **kwargs)
15 self.scorestring = tk.StringVar(self)
16 self.scorestring.set("0")
17 self.highscorestring = tk.StringVar(self)
18 self.highscorestring.set("0")
19 self.create_widgets()
20 self.canvas = tk.Canvas(self, width=410, height=410, borderwidth=5, highlightthickness=0)
21 self.canvas.pack(side="top", fill="both", expand="false")
22 self.new_game()
23
24
25 def addNewTile(self):
26 index = random.randint(0,6)
27 x = -1
28 y = -1
29 while self.isFull() == False:
30 x = random.randint(0,3)
31 y = random.randint(0,3)
32 if (self.board[x][y] == 0):
33 self.board[x][y] = self.new_tile_selection[index]
34 x1 = y*105
35 y1 = x*105
36 x2 = x1 + 105 - 5
37 y2 = y1 + 105 - 5
38 num = self.board[x][y]
39 if num == 2:
40 self.square[x,y] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#e0f2f8", tags="rect", outline="", width=0)
41 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#ffb459", text="2")
42 elif num == 4:
43 self.square[x,y] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#b8dbe5", tags="rect", outline="", width=0)
44 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#ffb459", text="4")
45
46
47 break
48 #Returns True if board is full
49 def isFull(self):
50 for i in range(0,4):
51 for j in range(0,4):
52 if (self.board[i][j] == 0):
53 return False
54 return True
55 #Prints game board
56 def printboard(self):
57 cellwidth = 105
58 cellheight = 105
59 self.square = {}
60
61 for column in range(4):
62 for row in range(4):
63 x1 = column*cellwidth
64 y1 = row*cellheight
65 x2 = x1 + cellwidth - 5
66 y2 = y1 + cellheight - 5
67 num = self.board[row][column]
68 if num == 0:
69 self.print0(row, column, x1, y1, x2, y2)
70 elif num == 2:
71 self.print2(row, column, x1, y1, x2, y2)
72 elif num == 4:
73 self.print4(row, column, x1, y1, x2, y2)
74 elif num == 8:
75 self.print8(row, column, x1, y1, x2, y2)
76 elif num == 16:
77 self.print16(row, column, x1, y1, x2, y2)
78 elif num == 32:
79 self.print32(row, column, x1, y1, x2, y2)
80 elif num == 64:
81 self.print64(row, column, x1, y1, x2, y2)
82 elif num == 128:
83 self.print128(row, column, x1, y1, x2, y2)
84 elif num == 256:
85 self.print256(row, column, x1, y1, x2, y2)
86 elif num == 512:
87 self.print512(row, column, x1, y1, x2, y2)
88 elif num == 1024:
89 self.print1024(row, column, x1, y1, x2, y2)
90 elif num == 2048:
91 self.print2048(row, column, x1, y1, x2, y2)
92
93 def print0(self, row, column, x1, y1, x2, y2):
94 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#f5f5f5", tags="rect", outline="")
95 def print2(self, row, column, x1, y1, x2, y2):
96 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#e0f2f8", tags="rect", outline="")
97 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#494949", text="2")
98 def print4(self, row, column, x1, y1, x2, y2):
99 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#b8dbe5", tags="rect", outline="")
100 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#494949", text="4")
101 def print8(self, row, column, x1, y1, x2, y2):
102 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#71b1bd", tags="rect", outline="")
103 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="white", text="8")
104 def print16(self, row, column, x1, y1, x2, y2):
105 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#27819f", tags="rect", outline="")
106 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="white", text="16")
107 def print32(self, row, column, x1, y1, x2, y2):
108 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#0073b9", tags="rect", outline="")
109 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="white", text="32")
110 def print64(self, row, column, x1, y1, x2, y2):
111 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#7fa8d7", tags="rect", outline="")
112 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="white", text="64")
113 def print128(self, row, column, x1, y1, x2, y2):
114 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#615ea6", tags="rect", outline="")
115 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 32), fill="white", text="128")
116 def print256(self, row, column, x1, y1, x2, y2):
117 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#2f3490", tags="rect", outline="")
118 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 32), fill="white", text="256")
119 def print512(self, row, column, x1, y1, x2, y2):
120 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#1c1862", tags="rect", outline="")
121 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 32), fill="white", text="512")
122 def print1024(self, row, column, x1, y1, x2, y2):
123 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#9c005d", tags="rect", outline="")
124 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 30), fill="white", text="1024")
125 def print2048(self, row, column, x1, y1, x2, y2):
126 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#c80048", tags="rect", outline="")
127 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 30), fill="white", text="2048")
128
129
130 def create_widgets(self):
131 self.buttonframe = tk.Frame(self)
132 self.buttonframe.grid(row=2, column=0, columnspan=4)
133 tk.Button(self.buttonframe, text = "New Game",command=self.new_game).grid(row=0, column=0)
134 tk.Label(self.buttonframe, text = "Score:").grid(row=0, column=1)
135 tk.Label(self.buttonframe, textvariable=self.scorestring).grid(row=0, column=2)
136 tk.Label(self.buttonframe, text = "Record:").grid(row=0, column=3)
137 tk.Label(self.buttonframe, textvariable=self.highscorestring).grid(row=0, column=4)
138 self.buttonframe.pack(side="top")
139
140
141 def keyPressed(self,event):
142 shift = 0
143 if event.keysym == 'Down':
144 for j in range(0,4):
145 shift = 0
146 for i in range(3,-1,-1):
147 if self.board[i][j] == 0:
148 shift += 1
149 else:
150 if i - 1 >= 0 and self.board[i-1][j] == self.board[i][j]:
151 self.board[i][j] *= 2
152 self.score += self.board[i][j]
153 self.board[i-1][j] = 0
154 elif i - 2 >= 0 and self.board[i-1][j] == 0 and self.board[i-2][j] == self.board[i][j]:
155 self.board[i][j] *= 2
156 self.score += self.board[i][j]
157 self.board[i-2][j] = 0
158 elif i == 3 and self.board[2][j] + self.board[1][j] == 0 and self.board[0][j] == self.board[3][j]:
159 self.board[3][j] *= 2
160 self.score += self.board[3][j]
161 self.board[0][j] = 0
162 if shift > 0:
163 self.board[i+shift][j] = self.board[i][j]
164 self.board[i][j] = 0
165 self.printboard()
166 self.addNewTile()
167 self.isOver()
168 elif event.keysym == 'Right':
169 for i in range(0,4):
170 shift = 0
171 for j in range(3,-1,-1):
172 if self.board[i][j] == 0:
173 shift += 1
174 else:
175 if j - 1 >= 0 and self.board[i][j-1] == self.board[i][j]:
176 self.board[i][j] *= 2
177 self.score += self.board[i][j]
178 self.board[i][j-1] = 0
179 elif j - 2 >= 0 and self.board[i][j-1] == 0 and self.board[i][j-2] == self.board[i][j]:
180 self.board[i][j] *= 2
181 self.score += self.board[i][j]
182 self.board[i][j-2] = 0
183 elif j == 3 and self.board[i][2] + self.board[i][1] == 0 and self.board[0][j] == self.board[3][j]:
184 self.board[i][3] *= 2
185 self.score += self.board[i][3]
186 self.board[i][0] = 0
187 if shift > 0:
188 self.board[i][j+shift] = self.board[i][j]
189 self.board[i][j] = 0
190 self.printboard()
191 self.addNewTile()
192 self.isOver()
193 elif event.keysym == 'Left':
194 for i in range(0,4):
195 shift = 0
196 for j in range(0,4):
197 if self.board[i][j] == 0:
198 shift += 1
199 else:
200 if j + 1 < 4 and self.board[i][j+1] == self.board[i][j]:
201 self.board[i][j] *= 2
202 self.score += self.board[i][j]
203 self.board[i][j+1] = 0
204 elif j + 2 < 4 and self.board[i][j+1] == 0 and self.board[i][j+2] == self.board[i][j]:
205 self.board[i][j] *= 2
206 self.score += self.board[i][j]
207 self.board[i][j+2] = 0
208 elif j == 0 and self.board[i][1] + self.board[i][2] == 0 and self.board[i][3] == self.board[i][0]:
209 self.board[i][0] *= 2
210 self.score += self.board[i][0]
211 self.board[i][3] = 0
212 if shift > 0:
213 self.board[i][j-shift] = self.board[i][j]
214 self.board[i][j] = 0
215 self.printboard()
216 self.addNewTile()
217 self.isOver()
218 elif event.keysym == 'Up':
219 for j in range(0,4):
220 shift = 0
221 for i in range(0,4):
222 if self.board[i][j] == 0:
223 shift += 1
224 else:
225 if i + 1 < 4 and self.board[i+1][j] == self.board[i][j]:
226 self.board[i][j] *= 2
227 self.score += self.board[i][j]
228 self.board[i+1][j] = 0
229 elif i + 2 < 4 and self.board[i+1][j] == 0 and self.board[i+2][j] == self.board[i][j]:
230 self.board[i][j] *= 2
231 self.score += self.board[i][j]
232 self.board[i+2][j] = 0
233 elif i == 0 and self.board[1][j] + self.board[2][j] == 0 and self.board[3][j] == self.board[0][j]:
234 self.board[0][j] *= 2
235 self.score += self.board[0][j]
236 self.board[3][j] = 0
237 if shift > 0:
238 self.board[i-shift][j] = self.board[i][j]
239 self.board[i][j] = 0
240 self.printboard()
241 self.addNewTile()
242 self.isOver()
243 self.scorestring.set(str(self.score))
244 if self.score > self.highscore:
245 self.highscore = self.score
246 self.highscorestring.set(str(self.highscore))
247
248
249 def new_game(self):
250 self.score = 0
251 self.scorestring.set("0")
252 self.board = []
253 self.board.append([0,0,0,0])
254 self.board.append([0,0,0,0])
255 self.board.append([0,0,0,0])
256 self.board.append([0,0,0,0])
257 while True:
258 x = random.randint(0,3)
259 y = random.randint(0,3)
260 if (self.board[x][y] == 0):
261 self.board[x][y] = 2
262 break
263
264 index = random.randint(0,6)
265 while self.isFull() == False:
266 x = random.randint(0,3)
267 y = random.randint(0,3)
268 if (self.board[x][y] == 0):
269 self.board[x][y] = self.new_tile_selection[index]
270 break
271 self.printboard()
272
273
274
275 def isOver(self):
276 for i in range(0,4):
277 for j in range(0,4):
278 if (self.board[i][j] == 2048):
279 self.youWon()
280 for i in range(0,4):
281 for j in range(0,4):
282 if (self.board[i][j] == 0):
283 return False
284 for i in range(0,4):
285 for j in range(0,3):
286 if (self.board[i][j] == self.board[i][j+1]):
287 return False
288 for j in range(0,4):
289 for i in range(0,3):
290 if self.board[i][j] == self.board[i+1][j]:
291 return False
292 gameover = [["G", "A", "M", "E",],["O", "V", "E", "R"], ["", "", "", ""], ["", "", "", ""]]
293 cellwidth = 105
294 cellheight = 105
295 self.square = {}
296
297 for column in range(4):
298 for row in range(4):
299 x1 = column*cellwidth
300 y1 = row*cellheight
301 x2 = x1 + cellwidth - 5
302 y2 = y1 + cellheight - 5
303 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#e0f2f8", tags="rect", outline="")
304 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#494949", text=gameover[row][column])
305 return True
306 def youWon(self):
307 gameover = [["Y", "O", "U", "",],["W", "O", "N", "!"], ["", "", "", ""], ["", "", "", ""]]
308 cellwidth = 105
309 cellheight = 105
310 self.square = {}
311 for column in range(4):
312 for row in range(4):
313 x1 = column*cellwidth
314 y1 = row*cellheight
315 x2 = x1 + cellwidth - 5
316 y2 = y1 + cellheight - 5
317 self.square[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="#e0f2f8", tags="rect", outline="")
318 self.canvas.create_text((x1 + x2)/2, (y1+y2)/2, font=("Arial", 36), fill="#494949", text=gameover[row][column])
319
320
321 if __name__ == "__main__":
322 app = Game()
323 app.bind_all('<Key>', app.keyPressed)
324 app.wm_title("2048 Game")
325 app.minsize(420,450)
326 app.maxsize(420,450)
327 app.mainloop()